// pages/search/search.js
const req = require('../../utils/apifm-wxapi')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    searchText: '',
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    flag:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.key)
    req.goods({ nameLike: options.key }).then((res) => {
      console.log(res)
      this.setData({
        list: res.data,
        searchText: options.key
      })
    })
    /**
         * 获取当前设备的宽高
         */
    var that = this;
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
  },
  //  tab切换逻辑
  swichNav: function (e) {
    console.log(this.data.searchText)
    console.log(e.target.dataset.current)
    var that = this;
    if(e.target.dataset.current==2){
      req.goods({orderBy:'ordersDown',nameLike: this.data.searchText}).then((res) => {
        console.log(res)
        this.setData({
          list: res.data
        })
      })
    }else if(e.target.dataset.current==3){
      req.goods({orderBy:'priceDown',nameLike: this.data.searchText}).then((res) => {
        console.log(res)
        this.setData({
          list: res.data
        })
      })
    }else{
      req.goods({nameLike: this.data.searchText}).then((res) => {
        console.log(res)
        this.setData({
          list: res.data
        })
      })
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    
  },
bom(){
  var flag=!this.data.flag
  this.setData({
    flag:flag
  })
},
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})